import os 
import time, datetime
import random
import requests


"""
    authors @ MicahelZhang

    input:
        a dict
    output::
        result dict 

"""

def send_message(message):
    # server : 
    api_url = "http://server_ip:9001/API102"
    # local : 
    # api_url = "http://0.0.0.0:5010/API102"

    try:
        response = requests.put(api_url, json=message)
        result = response.json()
        status = response.status_code
        print(status, "send_message out===", result)
        return status
    except Exception as e:
        print(e)
        return False

def check_work_status_by_work_id(work_id):
    name="check_work_status_by_work_id"
    message={"name":name,
             "work_id": work_id}
    result = send_message(message)
    return result

def submit_job_socket(work_info):
    name="newtask_job_submit"
    work_info["name"] = name
    message = work_info
    if debug:
        print(message)
    result = send_message(message)
    return result

def create_work_id():
    return str(int(time.time()))+str(random.randint(0,1000))


# """
# 提交计算任务测试模型
# 推建 使用 create_work_id() 函数获取work_id
# 所有提交计算任务都可以在 http://10.12.2.220:9000 这个平台上访问
# """


if __name__=='__main__':
    
    import random
    debug = True

    work_info = {"name": "newtask_job_submit"
        , "user_name": "blac"
        , "load_type": "A"
        , "work_name": "blac"
        , "work_id": "blac_20221113023551547"
        , "work_dir": "\\\\SERVER-xx\\blac_20221113023551547"
        , "wind_type": "yes"
        , "wind_path": ""
        , "DLC_sheet": "inputfile\\xxxxx.xls"
        , "pj": "inputfile\\powprod.$PJ"
        , "bladed_in": "inputfile\\dtbladed.in"
        , "dll": "inputfile\\Discon.dll"
        , "ctr_xml": "inputfile\\Parameters.xml"
        , "new_blade.xml":"blade.json"
        # , "blade_xml":"放新blade.json"
        , "new_airfoil":"airfoil.json"
        , "short_2": ""
        , "short_3": ""}

    debug = True
    sumit_sign = True

    if sumit_sign:
        work_info['work_id'] = create_work_id()
        result = submit_job_socket(work_info)
        print (" status is -------", result)
        # check work status
        status = check_work_status_by_work_id(work_info['work_id'])
        print (" status is -------", status)
        res = {"work_info":result,
               "status":status}
    # if debug:
    #     print(work_info["work_dir"])
        print(res)


